<template>
	<view class="container-root">
		<view class="change-userinfo-layer">
			{{changeType ==='0'? "手机号":"地址"}}<input class="change-userinfo-input" type="changeType ==='0'? 'number':'text'" @input="onChangeContent" />
		</view>
		<button class='change-userinfo-button' hover-class='change-userinfo-button-hover' @tap='onChangeAddressOrPhone'>提交</button>
		
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import apis from '@/api.js'
	import httpRequest from '@/utils/utilHttp.js'
	export default {
		data() {
			return {
				changeType:'0',
				changeContent:'',
			}
		},
		computed: {
			...mapState(['token', 'userInfo']), //对全局变量hasLogin进行监控
		},
		onLoad(Option)
		{
			this.changeType = Option.changetype;
			uni.setNavigationBarTitle({
				title: Option.changetype === "0"?"修改手机号码":"修改联系地址"
			})
		},
		methods: {
			...mapMutations(['logout']), //对全局方法 login进行监控
			
			onChangeContent(e){
				this.changeContent = e.detail.value;
			},
			onChangeAddressOrPhone(){
				let self = this;
				
				var parms = {
				    usercode: self.userInfo.userCode,
				    token:self.token,					
				}
				if(self.changeType ==='1')
				{
					parms.address = self.changeContent;
				}
				else if(self.changeContent.length == 11){
					parms.mobile = self.changeContent;
				}
				else{
					uni.showToast({
						title: '手机号格式不正确!请重新输入',
						icon: "none"
					})
					return;
				}
				/*
				apis.Updateuserinfo({
					"usercode": self.userInfo.userCode,
					"token":this.token,
					"mobile": self.changeContent
				}).then(res => {
					//console.log(res)
					if(res[1].data.status==200 ){
						if(self.changeType ==='1'){
							self.userInfo.address = self.changeContent;
						}
						else{
							self.userInfo.mobile = self.changeContent.substring(0, 3) + '****' + self.changeContent.substring(7, 11);
						}
						uni.navigateBack({});
					}
					else{
						uni.showToast({
						    title: "修改失败",
						    icon: "none"
						});
						
					}
				})*/
				let options = {
					url:httpRequest.requestUrl.updateUserInfo_post,
					method:httpRequest.requestMethod.POST,
					datas:parms
				}
				httpRequest.Request(options).then(res => {
					//console.log(res)
					if (res.data.status == 200) {
						if(self.changeType ==='1'){
							self.userInfo.address = self.changeContent;
						}
						else{
							self.userInfo.mobile = self.changeContent.substring(0, 3) + '****' + self.changeContent.substring(7, 11);
						}
						uni.navigateBack({});
					}
					else{
						uni.showToast({
						    title: "修改失败",
						    icon: "none"
						})
					}
				})
			}
			
		}
	}
</script>

<style>
	.change-userinfo-layer{
		height: 90rpx;
		width: calc(100% - 20rpx);
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		margin-top: 20rpx;
		padding-left: 20rpx;
		background-color: white;
	}
	.change-userinfo-input{
		flex:1;
		height: 100%;
		background-color: white;
		margin: 0rpx 20rpx 0rpx 20rpx;
	}
	.change-userinfo-button{
		width: calc(100% - 40rpx);
		height: 90rpx;
		display: flex;
		justify-content: center;
		align-items: center;		
		margin-top: 20rpx;
		background-color: #00b9ef;
		color: white;
	}
	.change-userinfo-button::after{
		border: none;
	}
	.change-userinfo-button-hover{
		background-color: rgba(0, 185, 239, 0.7); 
	}
</style>
